package org.prs.persistence.department;

import com.prs.util.PrsException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.prs.model.department.Department;
import org.prs.persistence.connector.MySQLDatabaseConnector;

public class DepartmentPersistenceManager {
    Connection conn;
    ResultSet result;
    public List<Department> getAllDepartments(){
        List<Department> departments=new ArrayList<Department>();
        PreparedStatement selectAllDepartments=null;
        conn=MySQLDatabaseConnector.getConnection();
        try{
           selectAllDepartments=conn.prepareStatement("select * from departments");
           result=selectAllDepartments.executeQuery();
           while(result.next()){
               departments.add(getDepartment(result));
           }
        }catch(SQLException e){
            throw  new PrsException(e);
        }
        return departments;
    }
    private Department getDepartment(ResultSet result){
        Department department=new Department();
        try{
            department.setId(result.getInt("id"));
            department.setName(result.getString("name"));
        }catch(SQLException e){
            throw new PrsException(e);
        }
        return department;
    }
}
